<?php
/**
 * @version		$Id: edit.php $
 * @package		Joomla.Administrator
 * @subpackage	com_gencode
 * @copyright	Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

// no direct access
defined('_JEXEC') or die;

JHtml::addIncludePath(JPATH_COMPONENT.'/helpers/html');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
?>
<script type="text/javascript">

window.addEvent('domready', function(){
	var el = document.id('join-form');

	//select-join-table-1
	document.id('select-table-container').addEvent('change:relay(select.select-join-table-1)', function(){
		var fields = this.getNext('span.fields-container');
		changeFields(fields, this.get('value'), 1)
	});

	document.id('select-table-container').addEvent('change:relay(select.select-join-table-2)', function(){
		var fields = this.getNext('span.fields-container');
		changeFields(fields, this.get('value'), 2)
	});

	document.id('btn-join-table').addEvent('click', function(){
		var html = $('tmpl-join-fields').get('html');

		var tableRowObject = new Element('li', { html: html, 'class': 'join-table' });
		tableRowObject.inject($('tmpl-join-fields'), 'before');

		return false;
	});

	document.id('select-table-container').addEvent('click:relay(a.del-join-table)', function(){
		this.getParent().destroy();
		return false;
	});

	<?php if(is_array($this->joins)): ?>
	<?php foreach ($this->joins as $key => $join): ?>
	var fields_1_<?php echo $key; ?> = document.id('join_table_on_1_<?php echo $key; ?>').getNext('span.fields-container');
	changeFields(fields_1_<?php echo $key; ?>, document.id('join_table_on_1_<?php echo $key; ?>').get('value'), 1, '<?php echo $join->join_field_on_1; ?>')
	<?php endforeach; ?>

	<?php foreach ($this->joins as $key => $join): ?>
	var fields_2_<?php echo $key; ?> = document.id('join_table_on_2_<?php echo $key; ?>').getNext('span.fields-container');
	changeFields(fields_2_<?php echo $key; ?>, document.id('join_table_on_2_<?php echo $key; ?>').get('value'), 2, '<?php echo $join->join_field_on_2; ?>')
	<?php endforeach; ?>
	<?php endif; ?>

	var elTableFields = document.id('list-table-fields');
	
	var requestGetTable = new Request({
	    url: '<?php echo JRoute::_('index.php?option=com_gencode&view=genmfront&tmpl=component', false)?>',
	    method: 'get',
	    onRequest: function(){ elTableFields.set('text', 'loading...'); },
	    onSuccess: function(responseText){
	    	elTableFields.set('html', responseText);

	    	//TODO: Must moved this snippet to genm_form.js
	    	if(document.id('jform_type').get('value') == 'form')
	    	{
		    	<?php
    			$i = 0;
    			if(is_array($this->item->form_fields))
    			{
	    			foreach ($this->item->form_fields as $formField): 
	    			?>
	
	    			var container_<?php echo $i; ?> = $('field_<?php echo $i; ?>').getNext('span');
	    			getFieldTypeConfig(container_<?php echo $i; ?>, document.id('field_<?php echo $i; ?>').get('value'), document.id('field_<?php echo $i; ?>').get('field'));
	
	    			<?php 
	    			$i ++;
	    			endforeach; 
	    		}
    			?>
	    	}
	    	//END
	    },
	    onFailure: function(){ elTableFields.set('html', 'Failure !'); }
	});

	var layout = 'table_fields';

    //add event onChange to combobox select table
    document.id('list-select-table').addEvent('change:relay(select)', function(){
        if(this.get('value') != '')
			requestGetTable.send('layout='+layout+'&table_id=' + this.get('value') + '&id=<?php echo $this->item->id; ?>' );
        else
            elTableFields.set('html', 'Select table');
	});

    if($$('#list-select-table select').get('value') != '')
        requestGetTable.send('layout='+layout+'&table_id=' + document.id('table_id').get('value') + '&id=<?php echo $this->item->id; ?>' );
});

function changeFields(fields, value, select_ordering, selected_value)
{
	var requestFields = new Request({
	    url: '<?php echo JRoute::_('index.php?option=com_gencode&view=genm&tmpl=component', false)?>',
	    method: 'get',
	    onRequest: function(){ fields.set('html', '<select class="width-110"><option>loading</option></select>'); },
	    onSuccess: function(responseText){
	    	fields.set('html', responseText);
	    },
	    onFailure: function(){ fields.set('html', 'Failure !'); }
	});

	requestFields.send('layout=fields&table=' + value + '&select_ordering=' + select_ordering + '&selected_value=' + selected_value);
}

	Joomla.submitbutton = function(task)
	{
		if (task == 'genmfront.cancel' || document.formvalidator.isValid(document.id('genmfront-form'))) {
			Joomla.submitform(task, document.getElementById('genmfront-form'));
		}
	}
</script>

<form action="<?php echo JRoute::_('index.php?option=com_gencode&layout=edit&id='.(int) $this->item->id); ?>" method="post" name="adminForm" id="genmfront-form" class="form-validate">
	<div class="width-100 fltlft">
		<fieldset class="adminform">
			<legend><?php echo empty($this->item->id) ? JText::_('COM_GENCODE_NEW_GENMFRONT') : JText::sprintf('COM_GENCODE_GENMFRONT_DETAILS', $this->item->id); ?></legend>
			<ul class="adminformlist">
				<li><?php echo $this->form->getLabel('name'); ?>
				<?php echo $this->form->getInput('name'); ?></li>

				<li><?php echo $this->form->getLabel('type'); ?>
				<?php echo $this->form->getInput('type'); ?></li>

				<li><?php echo $this->form->getLabel('created'); ?>
				<?php echo $this->form->getInput('created'); ?></li>
				
				<li><?php echo $this->form->getLabel('description'); ?>
				<?php echo $this->form->getInput('description'); ?></li>

				<li><?php echo $this->form->getLabel('id'); ?>
				<?php echo $this->form->getInput('id'); ?></li>
			</ul>
			
			<div class="clr"></div>
			
			<div id="join-list">
				<?php echo $this->loadTemplate('list'); ?>
			</div>
			
			<div class="clr"> </div>

		</fieldset>
	</div>

<div class="clr"></div>
<input type="hidden" name="task" value="" />
<?php echo JHtml::_('form.token'); ?>
</form>
